<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: soft01
  Date: 21-10-12
  Time: 下午6:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>用户管理</title>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/back-index.css" />
    <script src="${pageContext.request.contextPath}/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap-paginator.js"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap-mypaginator.js"></script>
    <script type="text/javascript" charset="utf-8">
        $(function() {
            let usernamebiaozhi =-1;
            let emailbiaozhi =-1;
            let rolebiaozhi =-1;
            $(".doModify").on("click", function() {
                $(".modal-title").html("用户修改");
                usernamebiaozhi=1;
                emailbiaozhi=1;
                rolebiaozhi=1;
                $("label[name='Msg']").text("");
                $("#xiugai").attr("disabled",false);

                $("#myModal").modal("show");
            });
            $(".updateOne").on("click", function() {
                $("#myModal").modal("hide");
            });

            // 显示隐藏查询列表
            $('#show-user-search').click(function() {
                $('#show-user-search').hide();
                $('#upp-user-search').show();
                $('.showusersearch').slideDown(500);
            });
            $('#upp-user-search').click(function() {
                $('#show-user-search').show();
                $('#upp-user-search').hide();
                $('.showusersearch').slideUp(500);
            });
            //同步分页
            //按钮的超链接设置函数
            myoptions.pageUrl = function(type, page, current) {
                return "${pageContext.request.contextPath }/user/sreachUserByForm2.do?pageNo="+page;
		      };
            myPaginatorFun("myPages",
                "${sessionScope.userListPage.pageNum==0?1:sessionScope.userListPage.pageNum}",
                "${sessionScope.userListPage.pages==0?1:sessionScope.userListPage.pages}");

            $("input[name='xiugai']").click(function () {
                let id= $(this).attr("UserId");
                console.log(id);
                $.ajax({
                    type:"get",
                    url:"${pageContext.request.contextPath}/user/findUserById.do?id="+id,
                    dataType:"json",
                    success:function (result) {
                        console.log(result)
                        console.log(result.responseCode)
                        if(result.responseCode=="2001"){
                            console.log("成功")
                            $("label[name=Msg]").text("")
                            $("#user_id").val(result.returnObject.id);
                            $("#username").val(result.returnObject.nickname);
                            $("#roleName").val(result.returnObject.role);
                            $("#email").val(result.returnObject.email);
                        }else{
                            console.log("失败")
                            $("#Mrg").text(result.message);
                        }
                    }
                })
            })

            $("#username").keyup(function () {
                let value=$(this).val();
                let This=$(this);
                $.ajax({
                    type:"post",
                    data:{
                        "nickname":value,
                    },
                    url:"${pageContext.request.contextPath}/user/jianchaUsername.do",
                    dataType:"json",
                    success:function (result) {
                        if(result.responseCode=="2001"){
                            usernamebiaozhi=1;
                            console.log("成功")
                            $("label[name='Msg']",This.parent().parent()).text("");
                        }else {
                            $("label[name='Msg']",This.parent().parent()).text(result.message);
                            usernamebiaozhi=-1;
                            $("#xiugai").attr("disabled",true);
                        }
                    }
                })
            });
            $("#roleName").keyup(function () {
                let value=$(this).val();
                let This=$(this);
                $.ajax({
                    type:"post",
                    data:{
                        "roleName":value,
                    },
                    url:"${pageContext.request.contextPath}/user/jianchaRoleName.do",
                    dataType:"json",
                    success:function (result) {
                        if(result.responseCode=="2001"){
                            rolebiaozhi=1;
                            $("label[name='Msg']",This.parent().parent()).text("");
                        }else {
                            $("label[name='Msg']",This.parent().parent()).text(result.message);
                            rolebiaozhi=-1
                            $("#xiugai").attr("disabled",true);
                        }
                    }
                })
            });

            $("#email").keyup(function () {
                let value=$(this).val();
                let This=$(this);
                let id=$("#user_id").val();
                $.ajax({
                    type:"post",
                    data:{
                        "email":value,
                        "id":id,
                    },
                    url:"${pageContext.request.contextPath}/user/jianchaEmail.do",
                    dataType:"json",
                    success:function (result) {
                        console.log(result.message)
                        if(result.responseCode=="2001"){
                            emailbiaozhi=1;
                            $("label[name='Msg']",This.parent().parent()).text("");
                        }else {
                            $("label[name='Msg']",This.parent().parent()).text(result.message);
                            emailbiaozhi=-1;
                            $("#xiugai").attr("disabled",true);
                        }
                    }
                })
            });

            $("#xiugai").click(function () {//修改提交
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/user/modifyUserById2.do",
                    data:$("#modifyUser").serialize(),
                    dataType:"json",
                    success:function (result) {
                        console.log(result);
                        if(result.responseCode=="2001"){
                            console.log(result)
                            console.log("修改成功")
                            $("#usernameMsg").text(" ")
                            $("#roleNameMsg").text(" ")
                            $("#emailMsg").text(" ")
                            location.href="${pageContext.request.contextPath}/user/sreachUserByForm2.do?pageNo=${sessionScope.userListPage.pageNum}"
                        }else {
                            console.log(result.responseCode);
                            $("#usernameMsg").text(result.message);
                        }
                    }
                })
            })
            $("input[name=jinyong]").click(function () {
                let id=$(this).attr("UserId");
                console.log(id)
                $.ajax({
                    type:"get",
                    url:"${pageContext.request.contextPath}/user/modifyStatusById.do?id="+id+"&status=-1",
                    dataType:"json",
                    success:function (result) {
                        if(result.responseCode=="2001"){
                            console.log(result)
                            console.log("修改成功")
                            $("#errorMsg1").text(" ")
                            location.href="${pageContext.request.contextPath}/user/sreachUserByForm2.do?pageNo=${sessionScope.userListPage.pageNum}"
                        }else {
                            console.log(result)
                            console.log("失败")
                            $("#errorMsg1").text(result.message)
                        }
                    }
                })
            })
            $("input[name=qiyong]").click(function () {
                let id=$(this).attr("UserId");
                $.ajax({
                    type:"get",
                    url:"${pageContext.request.contextPath}/user/modifyStatusById.do?id="+id+"&status=1",
                    dataType:"json",
                    success:function (result) {
                        if(result.responseCode=="2001"){
                            console.log(result)
                            console.log("修改成功")
                            $("#errorMsg1").text(" ")
                            location.href="${pageContext.request.contextPath}/user/sreachUserByForm2.do?pageNo=${sessionScope.userListPage.pageNum}"
                        }else {
                            console.log(result)
                            console.log("失败")
                            $("#errorMsg1").text(result.message)
                        }
                    }
                })
            })
            $("#doSearch").click(function () {
                $("#sreachForm").submit();
            })
            $("#myModal").mousemove(function () {
                if(emailbiaozhi==1 &&
                 usernamebiaozhi==1 &&
                emailbiaozhi ==1){
                    $("#xiugai").attr("disabled",false);
                }
            })

        });
        //ajax回调接收数据成功时再重新初始化分页按钮
        function ajaxLoadData(data, url, page){
            //TODO:
        }
    </script>

</head>

<body>
<div class="panel panel-default" id="userInfo">
    <div class="panel-heading">
        <h3 class="panel-title">用户管理</h3>
    </div>
    <div>
        <input type="button" value="查询" class="btn btn-success" id="doSearch"
               style="margin: 5px 5px 5px 15px;" /> <input type="button"
                                                           class="btn btn-primary" id="show-user-search" value="展开搜索" /> <input
            type="button" value="收起搜索" class="btn btn-primary"
            id="upp-user-search" style="display: none;" />
    </div>

    <div class="panel-body">
        <div class="showusersearch" style="display: none;">
            <form class="form-horizontal" id="sreachForm" action="${pageContext.request.contextPath}/user/sreachUserByForm.do" method="post">
                <div class="form-group">
                    <div class="form-group col-xs-6">
                        <label for="user-name" class="col-xs-3 control-label">登录名：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" id="user-name" name="username"
                                   placeholder="请输入登录名" />
                        </div>
                    </div>
                    <div class="form-group col-xs-6">
                        <label for="user-nickname" class="col-xs-3 control-label">昵称：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" id="user-nickname" name="userNickname"
                                   placeholder="请输入昵称" />
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-group col-xs-6">
                        <label for="user-email" class="col-xs-3 control-label">邮箱：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" id="user-email" name="email"
                                   placeholder="请输入邮箱" />
                        </div>
                    </div>
                    <div class="form-group col-xs-6">
                        <label for="role-name" class="col-xs-3 control-label">角色：</label>
                        <div class="col-xs-8">
                            <select class="form-control" id="role-name" name="role-name">
                                <option value="-1">全部</option>
                                <option value="103">普通</option>
                                <option value="101">管理员</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-group col-xs-6">
                        <label class="col-xs-3 control-label">开始日期：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" name="kaishitime"
                                   placeholder="请输入创建开始时间:2017-10-10" />
                        </div>
                    </div>
                    <div class="form-group col-xs-6">
                        <label class="col-xs-3 control-label">结束日期：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" name="jieshutime"
                                   placeholder="请输入创建结束时间:2017-10-12" />
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-group col-xs-6">
                        <label class="col-xs-3 control-label">开始日期：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" name="kaishitime1"
                                   placeholder="请输入登录开始时间:2017-10-10" />
                        </div>
                    </div>
                    <div class="form-group col-xs-6">
                        <label class="col-xs-3 control-label">结束日期：</label>
                        <div class="col-xs-8">
                            <input type="text" class="form-control" name="jieshutime1"
                                   placeholder="请输入登录结束时间:2017-10-12" />
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="show-list">
            <span id="Mrg" style="color: red">${sessionScope.Mrg}</span>
            <table class="table table-bordered table-hover"
                   style='text-align: center;'>
                <thead>
                <tr class="text-danger">
                    <th class="text-center">编号</th>
                    <th class="text-center">登录名</th>
                    <th class="text-center">角色</th>
                    <th class="text-center">昵称</th>
                    <th class="text-center">邮箱</th>
                    <th class="text-center">创建日期</th>
                    <th class="text-center">最近登录日期</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                <c:forEach items="${sessionScope.userListPage.list}" var="user" >
                <tr>
                    <td>${user.id}</td>
                    <td>${user.loginName}</td>
                    <td><c:choose>
                        <c:when test="${user.role=='1002'}">
                            普通用户
                        </c:when>
                        <c:otherwise>
                            管理员
                        </c:otherwise>
                    </c:choose></td>
                    <td>${user.nickname}</td>
                    <td>${user.email}</td>
                    <td>
                        <fmt:formatDate value="${user.createDate}" pattern="yyyy-MM-dd hh:mm:ss"/>
                    </td>
                    <td>
                        <fmt:formatDate value="${user.signDate}" pattern="yyyy-MM-dd hh:mm:ss"/>
                    </td>
                    <td class="text-center">
                        <input type="button" class="btn btn-warning btn-sm doModify" name="xiugai" UserId="${user.id}" value="修改" />
                        <c:choose>
                        <c:when test="${user.status==1}">
                        <input type="button" class="btn btn-danger btn-sm" value="禁用" name="jinyong" UserId="${user.id}"/></td>
                        </c:when>
                        <c:when test="${user.status==-1}">
                            <input type="button" class="btn btn-success btn-sm" value="启用" name="qiyong" UserId="${user.id}"/></td>
                        </c:when>
                    </c:choose>
                </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>

        <div id="errorMsg1" style="color: red"></div>

        <!-- 分页 -->
        <div style="text-align: center;">
            <ul id="myPages"></ul>
        </div>
        <div class="modal fade" tabindex="-1" id="myModal">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">用户修改</h4>
                    </div>
                    <div class="modal-body text-center">
                        <form id="modifyUser">
                        <div class="row text-right">
                            <label for="user_id" class="col-xs-4 control-label">编号：</label>
                            <div class="col-xs-4">
                                <input type="text" class="form-control" id="user_id" name="id"
                                       readonly="true" />
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="username" class="col-xs-4 control-label">昵称：</label>
                            <div class="col-xs-4">
                                <input type="text" class="form-control" id="username" name="username"/>
                            </div>
                            <label style="color: red" name="Msg"></label>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="roleName" class="col-xs-4 control-label">角色：</label>
                            <div class="col-xs-4">
                                <input type="text" class="form-control" id="roleName" name="roleName" />
                            </div>
                            <label style="color: red" name="Msg"></label>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="password" class="col-xs-4 control-label">密码：</label>
                            <div class="col-xs-4">
                                <input type="text" class="form-control" id="password" name="password"/>
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="email" class="col-xs-4 control-label">邮箱：</label>
                            <div class="col-xs-4">
                                <input type="email" class="form-control" id="email" name="email" />
                            </div>
                            <label style="color: red" name="Msg"></label>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-warning updateOne" id="xiugai" type="button">修改</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
        </form>
    </div>
</div>
</body>
</html>
